package com.tma.demoremoteservice;

import a.activity.ServiceUtil;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
    static final String TAG = "MainActivity";
    private TextView mStatusTextView;
    private Button mRefreshButton;
    private Button mStartButton;
    private Button mStopButton;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mStatusTextView = (TextView) findViewById(R.id.tv_service_status);
        mStartButton = (Button) findViewById(R.id.btn_start_service);
        mStopButton = (Button) findViewById(R.id.btn_stop_service);
        mRefreshButton = (Button) findViewById(R.id.btn_refesh_status);

        mStartButton.setOnClickListener(this);
        mStopButton.setOnClickListener(this);
        mRefreshButton.setOnClickListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        boolean running = ServiceUtil.isServiceRunning(this, MyRemoteService.class);
        updateServiceStatus(running);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_start_service:
            doStartService();
            break;
        case R.id.btn_stop_service:
            doStopService();
            break;
        case R.id.btn_refesh_status:
            boolean running = ServiceUtil.isServiceRunning(this, MyRemoteService.class);
            updateServiceStatus(running);
            break;
        default:
            break;
        }
    }

    private void updateServiceStatus(boolean running) {
        String status = null;
        if (running) {
            status = getString(R.string.running);
        } else {
            status = getString(R.string.stopped);
        }
        mStatusTextView.setText(status);
        mStopButton.setEnabled(running);
    }

    private void doStartService() {
        boolean running = ServiceUtil.isServiceRunning(this, MyRemoteService.class);

        if (running) {
            Toast.makeText(this, "The service is already started", Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent(IStockQuoteService.class.getName());
        startService(intent);

        updateServiceStatus(running);
    }

    private void doStopService() {
        Intent intent = new Intent(IStockQuoteService.class.getName());
        boolean success = stopService(intent);

        if (success) {
            mStopButton.setEnabled(false);
        }
    }
}
